import axios from 'axios'
import { message } from "ant-design-vue";
axios.defaults.headers["Content-Type"] = "application/json;charset=UTF-8";
const service = axios.create({
    baseURL: "http://localhost:8888/codes",
    timeout: 10000,
})

service.interceptors.request.use((req) => {
    return req;
})


service.interceptors.response.use((res) => {
    const { code, data } = res.data;
    const msg = res.data.message;
    if (code === 200) {
        return data;
    } else {
        message.error(msg);
        return Promise.reject(msg, 3);
    }
})


function request(options) {
    if (options.method.toLowerCase() === 'get') {
        options.params = options.data
    }

    const access = localStorage.getItem('access');
    if (options.token && options.token === true) {
        options.headers = {
            Authorization: "token:" + access, //指定消息格式
        }
    }

    return service(options)
}

export default request